package com.bee.beeAuth.config.general;

import cn.hutool.core.collection.CollectionUtil;
import org.springframework.http.HttpMethod;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Component;
import springfox.documentation.builders.OperationBuilder;
import springfox.documentation.builders.RequestParameterBuilder;
import springfox.documentation.builders.ResponseBuilder;
import springfox.documentation.schema.ScalarType;
import springfox.documentation.service.*;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spi.service.ApiListingScannerPlugin;
import springfox.documentation.spi.service.contexts.DocumentationContext;
import springfox.documentation.spring.web.readers.operation.CachingOperationNameGenerator;

import java.util.*;

/**
 * 自定义swagger接口
 * @description:
 * @author: chenguosong
 * @date: 2022-10-18 09:35
 */
@Component
public class SwaggerAddtion implements ApiListingScannerPlugin {

    @Override
    public List<ApiDescription> apply(DocumentationContext context) {
        //登录接口的传参
        RequestParameter username = new RequestParameterBuilder()
                .name("username").description("用户名").required(true)
                .in(ParameterType.QUERY).query(q -> q.model(m -> m.scalarModel(ScalarType.STRING)))
                .parameterIndex(1)
                .required(false).build();
        RequestParameter password = new RequestParameterBuilder()
                .name("password").description("密码").required(true)
                .in(ParameterType.QUERY).query(q -> q.model(m -> m.scalarModel(ScalarType.STRING)))
                .parameterIndex(2)
                .required(false).build();
        //登录接口的返参数
        Response response = new ResponseBuilder().code("200").description("OK").build();
        Set<String> set = new HashSet<String>();
        set.add("基础api");
        Operation loginOperation = new OperationBuilder(new CachingOperationNameGenerator())
                .method(HttpMethod.POST)
                .summary("登录")
                .tags(set)
                .responses(Collections.singleton(response))
                .consumes(CollectionUtil.newHashSet(MediaType.MULTIPART_FORM_DATA_VALUE))
                .produces(CollectionUtil.newHashSet(MediaType.APPLICATION_JSON_VALUE))
                .requestParameters(Arrays.asList(username, password))
                .build();

        ApiDescription loginDesc = new ApiDescription("", "/login", "登录","",
                Collections.singletonList(loginOperation), false);
        //退出
        Operation logoutOperation = new OperationBuilder(new CachingOperationNameGenerator())
                .method(HttpMethod.POST)
                .summary("登出")
                .notes("退出登录")
                .tags(set)
                .responses(Collections.singleton(response))
                .consumes(CollectionUtil.newHashSet(MediaType.MULTIPART_FORM_DATA_VALUE))
                .produces(CollectionUtil.newHashSet(MediaType.APPLICATION_JSON_VALUE))
                .build();
        ApiDescription logoutDesc =
                new ApiDescription("", "/logout", "注销","",
                Collections.singletonList(logoutOperation), false);

        List<ApiDescription> apiDescriptions = new ArrayList<>(Arrays.asList(loginDesc,logoutDesc));

        return apiDescriptions;
    }

    @Override
    public boolean supports(DocumentationType documentationType) {
        return DocumentationType.OAS_30.equals(documentationType);
    }
}